package com.yvon.service.demo.mybatis.session;

import com.yvon.service.demo.mybatis.binding.MapperRegistry;
import com.yvon.service.demo.mybatis.mapping.MappedStatement;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件解析以后会存放到 Configuration 配置类中，
 * 在配置类中添加映射器注册机和映射语句的存放
 *
 * @author yvon
 * @since 2023-03-21
 */
public class Configuration {

    /**
     * 映射器注册机
     */
    protected MapperRegistry mapperRegistry = new MapperRegistry(this);

    /**
     * 映射语句
     * key=id,value=SQL信息记录对象
     */
    protected final Map<String, MappedStatement> mappedStatements = new HashMap<>();

    public <T> void addMapper(Class<T> type) {
        mapperRegistry.addMapper(type);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
       return mapperRegistry.getMapper(type, sqlSession);
    }

    public boolean hasMapper(Class<?> type) {
        return mapperRegistry.hasMapper(type);
    }

    public void addMappedStatement(MappedStatement ms) {
        mappedStatements.put(ms.getId(), ms);
    }

    public MappedStatement getMappedStatement(String id) {
        return mappedStatements.get(id);
    }
}
